package person.aoao.library.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import person.aoao.library.dao.BookListDao;
import person.aoao.library.dao.BorrowListDao;
import person.aoao.library.entity.BookList;
import person.aoao.library.entity.BorrowList;
import person.aoao.library.result.ResponseCode;
import person.aoao.library.result.ResponseData;
import person.aoao.library.service.BorrowListService;
import person.aoao.library.vo.ListData;

import javax.annotation.Resource;
import java.util.List;

/**
 * (BorrowList)表服务实现类
 *
 * @author makejava
 * @since 2023-05-05 10:15:41
 */
@Service("borrowListService")
public class BorrowListServiceImpl implements BorrowListService {
    @Resource
    private BorrowListDao borrowListDao;
    @Resource
    private BookListDao bookListDao;


    /**
     * 通过ID查询单条数据
     *
     * @param borrowid 主键
     * @return 实例对象
     */
    @Override
    public BorrowList queryById(Integer borrowid) {
        return this.borrowListDao.queryById(borrowid);
    }

    /**
     * 分页查询
     *
     * @param borrowList  筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<BorrowList> queryByPage(BorrowList borrowList, PageRequest pageRequest) {
        long total = this.borrowListDao.count(borrowList);
        return new PageImpl<>(this.borrowListDao.queryAllByLimit(borrowList, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param borrowList 实例对象
     * @return 实例对象
     */
    @Override
    public BorrowList insert(BorrowList borrowList) {
        BookList book=this.bookListDao.queryById(borrowList.getBookid());
        book.setBookcountnow(book.getBookcountnow()-1);
        this.bookListDao.update(book);
        this.borrowListDao.insert(borrowList);
        return borrowList;
    }

    /**
     * 修改数据
     *
     * @param borrowList 实例对象
     * @return 实例对象
     */
    @Override
    public BorrowList update(BorrowList borrowList) {
        if(borrowList.getState()==0){
            BookList book=this.bookListDao.queryById(borrowList.getBookid());
            book.setBookcountnow(book.getBookcountnow()+1);
            this.bookListDao.update(book);
        }
        this.borrowListDao.update(borrowList);
        return this.queryById(borrowList.getBorrowid());
    }

    /**
     * 通过主键删除数据
     *
     * @param borrowid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer borrowid) {
        return this.borrowListDao.deleteById(borrowid) > 0;
    }

    @Override
    public ListData getBorrowList(String uid,Integer num,Integer page) {
        Long count=this.borrowListDao.countByUid(uid);
        List<BorrowList> list=this.borrowListDao.getBorrowListByUid(uid,num,(page-1)*num);
        for(BorrowList item :list){
            item.setBook(this.bookListDao.queryById(item.getBookid()));
        }
        ListData data=new ListData();
        data.setCount(count);
        data.setData(list);

        return data;
    }

    @Override
    public ResponseData queryByUid(String uid, Integer num, Integer page) {
        ListData data=new ListData();
        List<BorrowList> list=this.borrowListDao.getBorrowListByUid(uid,num,(page-1)*num);
        long count=this.borrowListDao.countByUid(uid);
        data.setData(list);
        data.setCount(count);
        return new ResponseData(ResponseCode.SUCCESS,data);
    }

    @Override
    public ResponseData selectAll(Integer num, Integer page) {
        ListData data=new ListData();
        List<BorrowList> list=this.borrowListDao.selectAll(num,(page-1)*num);
        long count=this.borrowListDao.selectCount();
        data.setData(list);
        data.setCount(count);
        return new ResponseData(ResponseCode.SUCCESS,data);
    }

    @Override
    public ResponseData searchBorrow(String keyWord, Integer state, Integer num, Integer page) {
        System.out.println(keyWord+""+state);
        List<BorrowList> list=this.borrowListDao.searchBorrow("%"+keyWord+"%",state,num,(page-1)*num);
        long count =this.borrowListDao.searchCount("%"+keyWord+"%",state);
        ListData data=new ListData();
        data.setCount(count);
        data.setData(list);
        return  new ResponseData(ResponseCode.SUCCESS,data);

    }


}
